package com.flink.ttest.cofig;

import com.alibaba.fastjson.JSON;
import org.apache.flink.api.common.functions.AggregateFunction;

import java.util.concurrent.atomic.DoubleAdder;

/**
 * @author: LCG
 * @date: 2022-06-28 14:52:17
 * @description: 计算价格的和
 **/
public class OrderPriceCount implements AggregateFunction<Order,Order,Order> {

    @Override
    public Order createAccumulator() {
        Order order=new Order();
        order.setOrderPrice(0.0);
        order.setOrderNum(0);
        order.setOrderTotalPrice(0.0);
        return order;
    }

    @Override
    public Order add(Order order, Order order2) {
        DoubleAdder doubleAdder=new DoubleAdder();
        doubleAdder.add(order2.getOrderTotalPrice());
        doubleAdder.add(order.getOrderPrice());
        order2.setOrderTotalPrice(doubleAdder.doubleValue());
        order2.setOrderNum(order2.getOrderNum()+1);
        System.out.println(JSON.toJSONString(order2));
        return order2;
    }

    @Override
    public Order getResult(Order order) {
        return order;
    }

    @Override
    public Order merge(Order order, Order acc1) {
        acc1.setOrderTotalPrice(acc1.getOrderTotalPrice()+order.getOrderTotalPrice());
        acc1.setOrderNum(acc1.getOrderNum()+order.getOrderNum());
        System.out.println("结果"+JSON.toJSONString(acc1));
        return acc1;
    }

}
